package org.sc.xl; /**
 * Copyright (c) 2019, ShangHai YTO Co., Ltd.
 * All right reserved.
 * <p>
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF YTO
 * CO., LTD.  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED
 * TO THIRD PARTIES, COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 * WITHOUT THE PRIOR WRITTEN PERMISSION OF YTO
 * CO., LTD.
 */

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.io.Files;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author FXL
 * @description: TODO 请在此添加描述
 * @reason: TODO ADD REASON(可选)
 * @date 2022/4/12 15:23
 * @since JDK 1.8
 */
public class Init {

    public static String CONTROLLER = "Controller";
    public static String FACADE = "Facade";
    public static String FACADE_IMPL = "FacadeImpl";
    public static String DAO = "dao";
    public static String MAPPER = "Mapper";
    public static String VO = "Vo";
    public static String SERVICE = "Service";


    public static final String GLOBAL_TEMPLATE = "/**\n" +
            " * Copyright (c) 2019, ShangHai YTO Co., Ltd.\n" +
            " * All right reserved.\n" +
            " * <p>\n" +
            " * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF YTO\n" +
            " * CO., LTD.  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED\n" +
            " * TO THIRD PARTIES, COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,\n" +
            " * WITHOUT THE PRIOR WRITTEN PERMISSION OF YTO\n" +
            " * CO., LTD.\n" +
            " */";

    public static final String DESCRIPTION = "航空件-全程时效";

    public static final String DATE = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

    public static String JAVA_TEMPLATE = "\n/**\n" +
            " * @author FXL\n" +
            " * @description: " + DESCRIPTION + "\n" +
            " * @reason:    \n" +
            " * @date " + DATE + "\n" +
            " * @since JDK 1.8\n" +
            " */";


    public static final List<String> waitCreateDir = Lists.newArrayList("controller", "facade", "mapper", "vo", "service");

    public static final String fileDirName = "airfullaging";

    public static final String CLASS = "class";
    public static final String INTERFACE = "interface";

    public static String CLASS_NAME_PREFIX = "AirFullAging";

    public static void main(String[] args) throws IOException {
        String rootPath = "D:\\project\\jscapp";
        String moduleName = "brc";
        File root = new File(rootPath, moduleName);
        dfs(root);
    }


    public static void dfs(File f) throws IOException {
        if (!f.isHidden() && f.isDirectory() && !f.getName().equals("target")) {
            //按指定的包下进行创建
            for (String tarDir : waitCreateDir) {
                if (!f.getPath().contains(tarDir)) {
                    continue;
                }
                //判断文件夹是否已经创建，否的话创建
                File targetDir = new File(f.getPath(), fileDirName);
                if (!targetDir.exists()) {
                    targetDir.mkdir();
                }
                String packagePath;
                String packageInfo;
                String classInfo;
                String info;
                if(!targetDir.getPath().contains("java")){
                    return;
                }
                switch (tarDir) {
                    case "controller":
                        //创建.java文件
                        File controllerFile = new File(targetDir.getPath(), CLASS_NAME_PREFIX + CONTROLLER + ".java");
                        if (controllerFile.exists()) {
                            return;
                        }
                        //1、写入包信息 2、写入类或者接口信息
                        packagePath = targetDir.getPath().split("java\\\\")[1].replaceAll("\\\\", ".");
                        packageInfo = "\n" + "package " + packagePath + ";\n";
                        // rest 接口信息
                        classInfo = JAVA_TEMPLATE + "\n" + "@RestController\n" +
                                "@RequestMapping(value = \"/\")\n" + "@Api(value = \"\")\n" +
                                "public " + CLASS + " " + CLASS_NAME_PREFIX + CONTROLLER + "{"
                                + "\n\n\n"
                                + "}";
                        info = GLOBAL_TEMPLATE + packageInfo + classInfo;
                        Files.write(info.getBytes(), controllerFile);
                        break;
                    case "facade":
                        //创建.java文件
                        String targetDirPath = targetDir.getPath();
                        File facadeFile;
                        // Facade 需要判断是接口还是实现类
                        if (targetDirPath.contains("client")) {
                            facadeFile = new File(targetDirPath, CLASS_NAME_PREFIX + FACADE + ".java");
                            classInfo = JAVA_TEMPLATE + "\n" +
                                    "public " + INTERFACE + " " + CLASS_NAME_PREFIX + FACADE + "{"
                                    + "\n\n\n"
                                    + "}";
                        } else {
                            facadeFile = new File(targetDirPath, CLASS_NAME_PREFIX + FACADE_IMPL + ".java");
                            char[] chars = CLASS_NAME_PREFIX.toCharArray();
                            classInfo = JAVA_TEMPLATE + "\n" +
                                    "@Service(" + String.valueOf(chars[0]).toLowerCase() + CLASS_NAME_PREFIX.substring(1) + FACADE + ")\n"
                                    + "@Slf4j\n"
                                    + "public " + CLASS + " " + CLASS_NAME_PREFIX + FACADE_IMPL + " implements " + CLASS_NAME_PREFIX + FACADE + "{"
                                    + "\n\n\n"
                                    + "}";
                        }
                        if (facadeFile.exists()) {
                            return;
                        }
                        //1、写入包信息 2、写入类或者接口信息
                        packagePath = targetDir.getPath().split("java\\\\")[1].replaceAll("\\\\", ".");
                        packageInfo = "\n" + "package " + packagePath + ";\n";
                        info = GLOBAL_TEMPLATE + packageInfo + classInfo;
                        Files.write(info.getBytes(), facadeFile);
                        break;
                    case "mapper":
                        //创建.java文件
                        File mapperFile = new File(targetDir.getPath(), CLASS_NAME_PREFIX + MAPPER + ".java");
                        if (mapperFile.exists()) {
                            return;
                        }
                        //1、写入包信息 2、写入类或者接口信息
                        packagePath = targetDir.getPath().split("java\\\\")[1].replaceAll("\\\\", ".");
                        packageInfo = "\n" + "package " + packagePath + ";\n";
                        char[] mapperChars = CLASS_NAME_PREFIX.toCharArray();
                        // rest 接口信息
                        classInfo = JAVA_TEMPLATE + "\n"
                                + "@Repository(\"" + String.valueOf(mapperChars[0]).toLowerCase() + CLASS_NAME_PREFIX.substring(1) + MAPPER + "\")\n"
                                + "@Mapper\n"
                                + "public " + INTERFACE + " " + CLASS_NAME_PREFIX + MAPPER + "{"
                                + "\n\n\n"
                                + "}";
                        info = GLOBAL_TEMPLATE + packageInfo + classInfo;
                        Files.write(info.getBytes(), mapperFile);
                        break;
                    case "service":
                        //创建.java文件
                        File serviceFile = new File(targetDir.getPath(), CLASS_NAME_PREFIX + SERVICE + ".java");
                        if (serviceFile.exists()) {
                            return;
                        }
                        //1、写入包信息 2、写入类或者接口信息
                        packagePath = targetDir.getPath().split("java\\\\")[1].replaceAll("\\\\", ".");
                        packageInfo = "\n" + "package " + packagePath + ";\n";
                        char[] serviceChars = CLASS_NAME_PREFIX.toCharArray();
                        // rest 接口信息
                        classInfo = JAVA_TEMPLATE + "\n"
                                + "@Service(\"" + String.valueOf(serviceChars[0]).toLowerCase() + CLASS_NAME_PREFIX.substring(1) + SERVICE + "\")\n"
                                + "public " + CLASS + " " + CLASS_NAME_PREFIX + SERVICE + "{"
                                + "\n\n\n"
                                + "}";
                        info = GLOBAL_TEMPLATE + packageInfo + classInfo;
                        Files.write(info.getBytes(), serviceFile);
                        break;
                    default:
                        break;
                }
                return;
            }
            for (int i = 0; i < f.listFiles().length; i++) {
                dfs(f.listFiles()[i]);
            }
        }
    }
}
